package com.tanhua.dubbo.api.impl.mongo;

import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.mongo.RecommendUser;
import com.tanhua.domain.mongo.UserLike;
import com.tanhua.domain.mongo.UserUnLike;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.TodayBestVo;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.mongo.RecommendUserApi;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

@Service
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Reference(timeout=10000)
    private UserInfoApi userInfoApi;

    /**
     * 今日佳人：
     *  db.recommend_user.find({userId:1}).sort({score:-1}).limit(1);
     * @param userId
     * @return
     */
    @Override
    public RecommendUser queryWithMaxScore(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        query.with(Sort.by(Sort.Order.desc("score"))).limit(1);
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        return recommendUser;
    }

    /**
     * 查询今日推荐佳人分页对象
     //第一页， 每页10条
     db.recommend_user.find({userId:1}).sort({score:-1}).limit(10).skip(0);
     //第二页， 每页10条
     db.recommend_user.find({userId:1}).sort({score:-1}).limit(10).skip(10);
     //第三页， 每页10条
     db.recommend_user.find({userId:1}).sort({score:-1}).limit(10).skip(20);
     */
    @Override
    public PageResult queryRecommendation(Integer page, Integer pagesize, Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        query.with(Sort.by(Sort.Order.desc("score")));
        query.limit(pagesize).skip((page-1)*pagesize);

        List<RecommendUser> recommendUserList = mongoTemplate.find(query, RecommendUser.class);
        Long count = mongoTemplate.count(query, RecommendUser.class);
        PageResult pageResult = new PageResult(page,pagesize,count.intValue(),recommendUserList);
        return pageResult;
    }

    @Override
    public Long queryScore(Long userId, Long recommendUserId) {
        Query query = new Query(Criteria.where("userId").is(userId).and("recommendUserId").is(recommendUserId));
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        if(recommendUser==null){
            return 60L;
        }
        return recommendUser.getScore().longValue();
    }

    //探花
    @Override
    public ArrayList<TodayBestVo> findTanHuaUser(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        List<RecommendUser> recommendUserList = mongoTemplate.find(query, RecommendUser.class);
        ArrayList<TodayBestVo> todayBestVos = new ArrayList<>();
        for (RecommendUser recommendUser : recommendUserList) {
            if (recommendUser==null){
                continue;
            }
            TodayBestVo todayBestVo = new TodayBestVo();
            Long recommendUserId = recommendUser.getRecommendUserId();
            //这个id是不是好友，这个id是不是我喜欢
            //判断是不是好友
            Query friendQuery = new Query(Criteria.where("userId").is(userId).and("friendId").is(recommendUserId));
            Friend friend = mongoTemplate.findOne(friendQuery, Friend.class);
            if (friend!=null){
                //存在就跳过，已经是我好友了
                continue;
            }
            //判断是不是我喜欢
            Query likeQuery = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(recommendUserId));
            UserLike userLike = mongoTemplate.findOne(likeQuery, UserLike.class);
            if (userLike!=null){
                //存在就跳过，已经是我喜欢了
                continue;
            }
            //不喜欢的id，应不应该有收集表tb_unlike
            Query unLikeQuery = new Query(Criteria.where("userId").is(userId).and("unLikeUserId").is(recommendUserId));
            UserUnLike unUserLike = mongoTemplate.findOne(unLikeQuery, UserUnLike.class);
            if (unUserLike!=null){
                //存在就跳过，已经是我不喜欢的了
                continue;
            }
            //重新读取推荐表中，是不是要剔除这些id
            UserInfo userInfo = userInfoApi.findById(recommendUserId);
            if (userInfo!=null){
                BeanUtils.copyProperties(userInfo,todayBestVo);
                if (userInfo.getTags()!=null){
                    String[] split = userInfo.getTags().split(",");
                    todayBestVo.setTags(split);
                }
            }
            todayBestVos.add(todayBestVo);
        }
        return todayBestVos;
    }
}
